<?php

error_reporting(0);

include dirname( __FILE__ )."/header.php";

$lid = isset($_GET['lid']) ? intval($_GET['lid']) : 0;
$cid = isset($_GET['cid']) ? intval($_GET['cid']) : 0;
if ( empty($lid) ) {
	die("No lid!");
} elseif ( empty($cid) ) {
	die("No cid!");
}

$result = $xoopsDB->query("select l.lid, l.cid, l.title, l.url, l.logourl, l.status, l.date, l.hits, l.rating, l.votes, l.comments, t.description from ".$xoopsDB->prefix($mydirname."_links")." l, ".$xoopsDB->prefix($mydirname."_text")." t where l.lid=$lid and l.lid=t.lid and status>0");

list($lid, $cid, $ltitle, $url, $logourl, $status, $time, $hits, $rating, $votes, $comments, $description) = $xoopsDB->fetchRow($result);

if ( empty($lid) ) {
	die("No data! Please check its cid and lid!");
}

$can_pdf = 0;
if ( $mylinks_can_pdf == 0 ) {
	$can_pdf = 0;
}
else if ( $mylinks_can_pdf == 1) {
	$can_pdf = 1;
}
else if ( $mylinks_can_pdf == 2) {
	if ( $xoopsUser ) {
	$can_pdf =1;
	}
	else {
	$can_pdf =0;
	}
}
else {
	$can_pdf = 0;
}

if ( empty($can_pdf) ) {
	die("Not allowed now!");
}

$myts =& MyTextSanitizer::getInstance();
//require_once XOOPSMYLINKPATH.'/fpdf/fpdf.inc.php';
require_once dirname( __FILE__ )."/fpdf/fpdf.inc.php";

$pdf_data['title'] = $myts->makeTboxData4Show($ltitle);
$pdf_data['subtitle'] = "";

$pdf_data['subsubtitle'] = $myts->makeTboxData4Show($url);
$pdf_data['date'] = formatTimestamp($time);
$pdf_data['filename'] = '';
$pdf_data['content'] = $myts->makeTareaData4Show($description,0);

$pdf_data['author'] = "";

//Other stuff
$puff='<br />';
$puffer='<br /><br /><br />';

//create the A4-PDF...
$pdf_config['slogan']=$xoopsConfig['sitename'].' - '.$xoopsConfig['slogan'];


$pdf=new PDF();
if(method_exists($pdf, "encoding")){
	$pdf->encoding($pdf_data, _CHARSET);
}
$pdf->SetCreator($pdf_config['creator']);
$pdf->SetTitle($pdf_data['title']);
$pdf->SetAuthor($pdf_config['url']);
$pdf->SetSubject($pdf_data['author']);
$out=$pdf_config['url'].', '.$pdf_data['author'].', '.$pdf_data['title'].', '.$pdf_data['subtitle'].', '.$pdf_data['subsubtitle'];
$pdf->SetKeywords($out);
$pdf->SetAutoPageBreak(true,25);
$pdf->SetMargins($pdf_config['margin']['left'],$pdf_config['margin']['top'],$pdf_config['margin']['right']);
$pdf->Open();

//First page
$pdf->AddPage();
$pdf->SetXY(24,25);
$pdf->SetTextColor(10,60,160);
$pdf->SetFont($pdf_config['font']['slogan']['family'],$pdf_config['font']['slogan']['style'],$pdf_config['font']['slogan']['size']);
$pdf->WriteHTML($pdf_config['slogan'], $pdf_config['scale']);
//$pdf->Image($pdf_config['logo']['path'],$pdf_config['logo']['left'],$pdf_config['logo']['top'],$pdf_config['logo']['width'],$pdf_config['logo']['height'],'',$pdf_config['url']);
$pdf->Line(25,30,190,30);
$pdf->SetXY(25,35);
$pdf->SetFont($pdf_config['font']['title']['family'],$pdf_config['font']['title']['style'],$pdf_config['font']['title']['size']);
$pdf->WriteHTML($pdf_data['title'],$pdf_config['scale']);

if ($pdf_data['subtitle']<>''){
	$pdf->WriteHTML($puff,$pdf_config['scale']);
	$pdf->SetFont($pdf_config['font']['subtitle']['family'],$pdf_config['font']['subtitle']['style'],$pdf_config['font']['subtitle']['size']);
	$pdf->WriteHTML($pdf_data['subtitle'],$pdf_config['scale']);
}
if ($pdf_data['subsubtitle']<>'') {
	$pdf->WriteHTML($puff,$pdf_config['scale']);
	$pdf->SetFont($pdf_config['font']['subsubtitle']['family'],$pdf_config['font']['subsubtitle']['style'],$pdf_config['font']['subsubtitle']['size']);
	$pdf->WriteHTML($pdf_data['subsubtitle'],$pdf_config['scale']);
}

$pdf->WriteHTML($puff,$pdf_config['scale']);
$pdf->SetFont($pdf_config['font']['author']['family'],$pdf_config['font']['author']['style'],$pdf_config['font']['author']['size']);
$out='';
$out.=$pdf_data['author'];
$pdf->WriteHTML($out,$pdf_config['scale']);
$pdf->WriteHTML($puff,$pdf_config['scale']);
$out='Date:';
$out.=$pdf_data['date'];
$pdf->WriteHTML($out,$pdf_config['scale']);
$pdf->WriteHTML($puff,$pdf_config['scale']);

$pdf->SetTextColor(0,0,0);
$pdf->WriteHTML($puffer,$pdf_config['scale']);

$pdf->SetFont($pdf_config['font']['content']['family'],$pdf_config['font']['content']['style'],$pdf_config['font']['content']['size']);
$pdf->WriteHTML($pdf_data['content'],$pdf_config['scale']);

//$pdf->Output($pdf_data['filename'],'');
$pdf->Output();
?>